//
//  GeexLoadingView.m
//  VideoRecordManager
//
//  Created by WangYonghe on 2020/5/6.
//  Copyright © 2020 WangYonghe. All rights reserved.
//

#import "GeexLoadingView.h"
#import "FLAnimatedImage.h"
#import "FLAnimatedImageView.h"

static GeexLoadingView *_waittingFullView = nil;

@interface GeexLoadingView()

@property(nonatomic,strong) UIView *loadingView;

@end

@implementation GeexLoadingView

+ (instancetype)sharedInstance
{
    
    static dispatch_once_t oneOver;
    dispatch_once( &oneOver, ^{
        _waittingFullView = [[GeexLoadingView alloc] init];
    });
    return _waittingFullView;
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    if (self){
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.15];
        self.hidden = YES;
        [[UIApplication sharedApplication].keyWindow addSubview:self];
      
        NSString *path = [[NSBundle mainBundle] pathForResource:@"GeexVideoBundle.bundle/geex_loading" ofType:@"gif"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        FLAnimatedImageView *gifImageView = [[FLAnimatedImageView alloc] init];
        gifImageView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2-120-20, 0, 240, 180);
        FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data];
        gifImageView.animatedImage = image;

        self.loadingView = [[UIView alloc]initWithFrame:CGRectMake(20, [[UIScreen mainScreen] bounds].size.height/2-90, [[UIScreen mainScreen] bounds].size.width-40, 180)];
        [self.loadingView addSubview:gifImageView];
        
        [self addSubview:self.loadingView];
        
    }
    return self;
}



- (void)showWaittingView{
    if (![[[UIApplication sharedApplication].delegate window].subviews containsObject:self]){
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    }
    
    //self.loadingView.frame = CGRectMake(self.loadingView.frame.origin.x, self.loadingView.frame.origin.y, self.loadingView.frame.size.width, [[UIScreen mainScreen] bounds].size.height/2-90);
    
    self.alpha = 0.0;
    self.hidden = NO;
    [UIView animateWithDuration:0.05 animations:^{
         self.alpha = 1.0;
    }];
}

- (void)hideWaittingView{
    self.hidden = YES;
    [self removeFromSuperview];
}

@end
